/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 *****************************************************************************/

#include "WaveformContainer.h"

#include <numeric>

// Przestrzeń nazw
namespace RafMat
{
namespace SimEngine
{

using namespace std;
using namespace SimInterface;

WaveformContainer::WaveformContainer(ISimulator &isim)
    : m_simulator(isim)
{
}

void WaveformContainer::insertWaveform(WaveformBaseSPtr waveform)
{
    m_waveforms.push_back(waveform);
}

void WaveformContainer::removeWaveform(WaveformBaseSPtr waveform)
{
    std::vector<WaveformBaseSPtr>::iterator it;
    it = remove(m_waveforms.begin(), m_waveforms.end(), waveform);
    m_waveforms.resize(it - m_waveforms.begin());
}

namespace
{
double accumulateFunc(double acc, WaveformBaseSPtr wfrm)
{
    return acc + wfrm->getNextValue();
}
}

double WaveformContainer::currentValue() const
{
    return accumulate(m_waveforms.begin(), m_waveforms.end(), double(0), accumulateFunc);
}

SimFile::ConfigWriter& WaveformContainer::dumpParameters(SimFile::ConfigWriter& o) const
{
    vector<WaveformBaseSPtr>::const_iterator it;

    for(it = m_waveforms.begin(); it != m_waveforms.end(); ++it)
    {
        // Wyślij konfigurację wejścia.
        o << *it->get();
    }

    return o;
}

}
}
